$(function() {
    $(".grid-myt-sale-box-order-detail-early-warning").data("gridOptions", {
        calcRowAmount : function(rowdata, src) {
            if (src == 'discountAmount') {
                rowdata['actualAmount'] = MathUtil.sub(rowdata['originalAmount'], rowdata['discountAmount']);
            } else {
                rowdata['discountAmount'] = MathUtil.sub(rowdata['originalAmount'], rowdata['actualAmount']);
            }
        },
        updateRowAmount : function(src) {
            var $grid = $(this);
            var rowdata = $grid.jqGrid("getEditingRowdata");
            $grid.data("gridOptions").calcRowAmount.call($grid, rowdata, src);
            $grid.jqGrid("setEditingRowdata", rowdata);
        },
        url : WEB_ROOT + '/myt/sale/box-order-detail!getEarlyWarningList',
        colModel : [ {
            label : '订单号',
            name : 'boxOrder.orderSeq',
            width : 200,
            align : 'left',
            formatter : function(cellValue, options, rowdata, action) {
                var html = cellValue;
                if (rowdata.circle) {
                    html += '<span class="badge badge-info">周</span>'
                }
                if (rowdata.splitPayMode != 'NO') {
                    html += '<span class="badge badge-success">分</span>'
                }
                return html;
            }
        }, {
            label : '状态',
            name : 'boxOrder.orderStatus',
            align : 'center',
            stype : 'select',
            searchoptions : {
                value : Util.getCacheEnumsByType('boxOrderStatusEnum')
            },
            width : 80
        }, {
            label : '下单时间',
            name : 'boxOrder.orderTime',
            align : 'left',
            sorttype : 'date'
        }, {
            label : '下单客户',
            name : 'boxOrder.customerProfile.display',
            index : 'boxOrder.customerProfile.trueName_OR_boxOrder.customerProfile.nickName_OR_boxOrder.customerProfile.id',
            align : 'left',
            width : 80,
            formatter : function(cellValue, options, rowdata, action) {
                var url = "${base}/myt/customer/customer-profile!view?id=" + rowdata.boxOrder.customerProfile.id;
                return '<a href="' + url + '" data-toggle="modal-ajaxify" title="客户资料">' + cellValue + '</a>'
            }
        }, {
            label : '收货人',
            name : 'boxOrder.receivePerson',
            width : 50,
            align : 'left'
        }, {
            label : '行项号',
            name : 'sn',
            align : 'center',
            width : 60
        }, {
            label : '预约发货日期',
            name : 'reserveDeliveryTime',
            editable : true,
            align : 'center',
            stype : 'date'
        }, {
            label : '行项状态',
            name : 'orderDetailStatus',
            width : 80,
            align : 'center',
            stype : 'select',
            searchoptions : {
                value : Util.getCacheEnumsByType('boxOrderDetailStatusEnum')
            }
        }, {
            label : '原始金额',
            name : 'originalAmount',
            formatter : 'currency',
            editable : true,
            editoptions : {
                dataInit : function(elem) {
                    var $elem = $(elem);
                    $elem.attr("readonly", true);
                }
            }
        }, {
            label : '折扣金额',
            name : 'discountAmount',
            editable : true,
            formatter : 'currency',
            editrules : {
                required : true,
            },
            editoptions : {
                dataInit : function(elem) {
                    var $grid = $(this);
                    var $elem = $(elem);
                    $elem.keyup(function() {
                        $grid.data("gridOptions").updateRowAmount.call($grid, $elem.attr("name"));
                    });
                }
            }
        }, {
            label : '应付金额',
            name : 'actualAmount',
            editable : true,
            formatter : 'currency',
            editrules : {
                required : true,
            },
            editoptions : {
                dataInit : function(elem) {
                    var $grid = $(this);
                    var $elem = $(elem);
                    $elem.keyup(function() {
                        $grid.data("gridOptions").updateRowAmount.call($grid, $elem.attr("name"));
                    });
                }
            }
        }, {
            label : '已付金额',
            name : 'payAmount',
            formatter : 'currency'
        } ],
        sortorder : "asc",
        sortname : "reserveDeliveryTime",
        postData : {
            "search['FETCH_customerProfile']" : "INNER",
            "search['FETCH_boxOrder']" : "INNER"
        },
        inlineNav : {
            add : false
        },
        setGroupHeaders : {
            groupHeaders : [ {
                startColumnName : 'boxOrder.orderSeq',
                numberOfColumns : 5,
                titleText : '订单信息'
            } ]
        },
        editurl : WEB_ROOT + '/myt/sale/box-order-detail!doSave',
        subGrid : true,
        subGridRowExpanded : function(subgrid_id, row_id) {
            Grid.initSubGrid(subgrid_id, row_id, {
                calcRowAmount : function(rowdata, src) {
                    if (src == 'discountAmount') {
                        rowdata['actualAmount'] = MathUtil.sub(rowdata['originalAmount'], rowdata['discountAmount']);
                        rowdata['modifiedPrice'] = MathUtil.div(rowdata['actualAmount'], rowdata['quantity']);
                    } else if (src == 'actualAmount') {
                        rowdata['discountAmount'] = MathUtil.sub(rowdata['originalAmount'], rowdata['actualAmount']);
                        rowdata['modifiedPrice'] = MathUtil.div(rowdata['actualAmount'], rowdata['quantity']);
                    } else if (src == 'modifiedPrice') {
                        rowdata['actualAmount'] = MathUtil.mul(rowdata['quantity'], rowdata['modifiedPrice']);
                        rowdata['discountAmount'] = MathUtil.sub(rowdata['originalAmount'], rowdata['actualAmount']);
                    } else {
                        rowdata['actualAmount'] = MathUtil.mul(rowdata['modifiedPrice'], rowdata['quantity']);
                        rowdata['discountAmount'] = MathUtil.sub(rowdata['originalAmount'], rowdata['actualAmount']);
                    }
                },
                updateRowAmount : function(src) {
                    var $grid = $(this);
                    var rowdata = $grid.jqGrid("getEditingRowdata");
                    $grid.data("gridOptions").calcRowAmount.call($grid, rowdata, src);
                    $grid.jqGrid("setEditingRowdata", rowdata);
                },

                url : WEB_ROOT + '/myt/sale/box-order-detail!boxOrderDetailCommodities?id=' + row_id,
                colModel : [ {
                    label : '商品',
                    name : 'commodity.display',
                    width : 250
                }, {
                    label : '销售单价',
                    name : 'price',
                    editable : true,
                    editoptions : {
                        dataInit : function(elem) {
                            var $elem = $(elem);
                            $elem.attr("readonly", true);
                        }
                    },
                    formatter : 'currency',
                    width : 100
                }, {
                    label : '销售数量',
                    name : 'quantity',
                    editable : true,
                    formatter : 'number',
                    width : 100,
                    editrules : {
                        required : true,
                        number : true
                    },
                    editoptions : {
                        dataInit : function(elem) {
                            var $grid = $(this);
                            var $elem = $(elem);
                            $elem.keyup(function() {
                                $grid.data("gridOptions").updateRowAmount.call($grid);
                            });
                        }
                    }
                }, {
                    label : '原始金额',
                    name : 'originalAmount',
                    formatter : 'currency',
                    editable : true,
                    editoptions : {
                        dataInit : function(elem) {
                            var $elem = $(elem);
                            $elem.attr("readonly", true);
                        }
                    },
                    width : 100
                }, {
                    label : '修改单价',
                    name : 'modifiedPrice',
                    formatter : 'currency',
                    editable : true,
                    editrules : {
                        number : true
                    },
                    editoptions : {
                        dataInit : function(elem) {
                            var $grid = $(this);
                            var $elem = $(elem);
                            $elem.keyup(function() {
                                $grid.data("gridOptions").updateRowAmount.call($grid, $elem.attr("name"));
                            });
                        }
                    },
                    width : 100
                }, {
                    label : '折扣金额',
                    name : 'discountAmount',
                    editable : true,
                    editrules : {
                        required : true
                    },
                    editoptions : {
                        dataInit : function(elem) {
                            var $grid = $(this);
                            var $elem = $(elem);
                            $elem.keyup(function() {
                                $grid.data("gridOptions").updateRowAmount.call($grid, $elem.attr("name"));
                            });
                        }
                    },
                    formatter : 'currency'
                }, {
                    label : '金额小计',
                    name : 'actualAmount',
                    editable : true,
                    editrules : {
                        required : true
                    },
                    editoptions : {
                        dataInit : function(elem) {
                            var $grid = $(this);
                            var $elem = $(elem);
                            $elem.keyup(function() {
                                $grid.data("gridOptions").updateRowAmount.call($grid, $elem.attr("name"));
                            });
                        }
                    },
                    formatter : 'currency'
                }, {
                    label : '已发货数量',
                    name : 'deliveriedQuantity',
                    formatter : 'number',
                    width : 80
                }, {
                    label : '发货仓库',
                    name : 'storageLocation.id',
                    width : 120,
                    editable : true,
                    stype : 'select',
                    searchoptions : {
                        value : Biz.getStockDatas()
                    }
                }, {
                    label : '锁定库存量',
                    name : 'salingLockedQuantity',
                    formatter : 'number',
                    width : 80,
                    editable : true,
                    editoptions : {
                        dataInit : function(el) {
                            var $el = $(el);
                            var $grid = $(this);
                            var rowid = $el.closest("tr.jqgrow").attr("id");
                            var rowdata = $grid.jqGrid("getRowData", rowid);
                            if ($el.val() == '') {
                                $el.val(rowdata['quantity']);
                            }
                        }
                    }
                } ],
                cmTemplate : {
                    sortable : false
                },
                editurl : WEB_ROOT + '/myt/sale/box-order-detail-commodity!doSave',
                rowNum : -1,
                toppager : false,
                filterToolbar : false,

            });
        }
    });
});